package com.example.weatherapp1.workmanager

import android.content.Context
import androidx.startup.Initializer
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import java.util.concurrent.TimeUnit

class WorkManagerInitializer : Initializer<Unit> {
    override fun create(context: Context) {
        // 创建周期性工作请求，每1小时执行一次
        val weatherUpdateWork = PeriodicWorkRequestBuilder<WeatherUpdateWorker>(
            1, TimeUnit.HOURS
        ).build()

        // 调度工作
        WorkManager.getInstance(context).enqueueUniquePeriodicWork(
            "weather_update_work",
            ExistingPeriodicWorkPolicy.KEEP,
            weatherUpdateWork
        )
    }

    override fun dependencies(): List<Class<out Initializer<*>>> {
        return emptyList()
    }
}
